package nina.yiyou.attendance.s20sum.input

import nina.yiyou.attendance.s10merge.compute.AttendDetailMerger

import java.util.stream.Collectors

class AttendDetailCsvReader {

  interface Line {

    String employeeId()

    double workingHour()

    double totalHourProba()

    double wageHourProba()

    double totalHourFull()

    double wageHourFull()

    double gainOff()

    int forgotCount()

    int lateCount()

    int workOvertime()

    int mealStipend()

    Map<String, Double> leaveMap()

    Map<String, ?> inputMap()
  }

  AttendDetailCsvReader(List<AttendDetailMerger.DayResult> attendDetailList) {
    _attendDetailList = attendDetailList
  }

  List<Line> read() {
    return _attendDetailList.stream()
        .map { makeLine(it) }
        .collect(Collectors.toList())
  }

  private Line makeLine(AttendDetailMerger.DayResult day) {
    String employeeId = day.employeeId()
    assert employeeId

    Map<String, ?> input = day.toMap()
    def key = { Map.Entry<String, ?> e -> e.key.split('\\.')[1] }
    def val = { Map.Entry<String, ?> e -> e.value }
    Map leaveMap = input.entrySet().stream()
        .filter { it.key.startsWith('请假.') }
        .collect(Collectors.toMap(key, { val(it) as Double }))

    return new Line() {
      @Override
      String employeeId() {
        return employeeId
      }

      @Override
      double workingHour() {
        return input['计算.时长'] as double
      }

      @Override
      double totalHourProba() {
        return input['计算.试用应计'] as double
      }

      @Override
      double wageHourProba() {
        return input['计算.试用实计'] as double
      }

      @Override
      double totalHourFull() {
        return input['计算.转正应计'] as double
      }

      @Override
      double wageHourFull() {
        return input['计算.转正实计'] as double
      }

      @Override
      int lateCount() {
        return input['计算.迟到分钟'] as int
      }

      @Override
      int forgotCount() {
        return input['计算.忘打卡'] as int
      }

      @Override
      double gainOff() {
        return input['计算.获得调休'] as double
      }

      @Override
      int workOvertime() {
        return input['计算.加班'] as int
      }

      @Override
      int mealStipend() {
        return input['计算.餐补'] as int
      }

      @Override
      Map<String, Double> leaveMap() {
        return leaveMap
      }

      @Override
      Map<String, ?> inputMap() {
        return input
      }
    }
  }

  private final List<AttendDetailMerger.DayResult> _attendDetailList
}
